package com.ele.server.util;

import com.ele.model.Answer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AnswersGeneratorImpl implements AnswersGenerator {

    private static List<String> auxiliaryVerbs = new ArrayList<String>();
    private static List<String> interrogativePronouns = new ArrayList<String>();
    private static List<String> articles = new ArrayList<String>();
    private static List<String> nouns = new ArrayList<String>();
    private static List<String> adjectives = new ArrayList<String>();
    private static Map<String, String> antonyms = new HashMap<String, String>();
    private static Map<String, String> negatives = new HashMap<String, String>();
    private static Map<String, String> contraries = new HashMap<String, String>();
    private static List<String> pronouns = new ArrayList<String>();

    public static final String PLACEHOLDER = "...";

    static {
        auxiliaryVerbs.add("is");

        interrogativePronouns.add("what");
        interrogativePronouns.add("which");


        articles.add("a");
        articles.add("an");
        articles.add("the");

        nouns.add("chair");

        adjectives.add("high");
        adjectives.add("low");

        antonyms.put("high", "low");

        negatives.put("is", "is not");

        contraries.put("is not", "isn't");

        pronouns.add("it");
    }

    @Override
    public List<Answer> generate(String question) {
        question = question.toLowerCase();


        for (String pronoun : interrogativePronouns) {
            for (String verb : auxiliaryVerbs) {
                if (question.startsWith(pronoun + " " + verb) || question.startsWith(contrary(pronoun, verb))) {
                    if (pronoun.equals("which")) {
                        return which(question, verb);
                    }
                }
            }
        }
        for (String verb : auxiliaryVerbs) {
            if (question.startsWith(verb + " ")) {
                if (verb.equals(auxiliaryVerbs.get(0))) {
                    return byIs(question);
                }
            }
        }

        return null;
    }

    private List<Answer> which(String question, String verb) {
        String pronoun = "which";

        String base = question.replaceFirst(pronoun + " " + verb + " ", "");
        base = base.replaceFirst(contrary(pronoun, verb) + " ", "");
        base = base.substring(0, base.length() - 1);

        List<Answer> answers = new ArrayList<Answer>();

        Answer a1 = new Answer();
        a1.setText(PLACEHOLDER + " " + verb + " " + base + "");
        answers.add(a1);

        Answer a2 = new Answer();
        a2.setText(contrary(PLACEHOLDER, verb) + " " + base + "");
        answers.add(a2);

        return answers;
    }

    private List<Answer> byIs(String question) {
        String verb = "is";

        List<Answer> answers = new ArrayList<Answer>();
        for (String adjective : adjectives) {
            if (question.endsWith(adjective + "?")) {
                int start = verb.length() + 1;
                int end = question.indexOf(adjective) - 1;

                String base = question.substring(start, end);
                String pronoun = getPronoun(base);

                Answer a1 = new Answer();
                a1.setText("Yes, " + base + " " + verb + " " + adjective + "");
                answers.add(a1);

                Answer a4 = new Answer();
                a4.setText("Yes, " + contrary(base, verb) + " " + adjective + "");
                answers.add(a4);

                Answer a2 = new Answer();
                a2.setText("No, " + base + " " + negatives.get(verb) + " " + adjective + ", but " + pronoun + " " + verb + " " + antonym(adjective) + "");
                answers.add(a2);

                Answer a3 = new Answer();
                a3.setText("No, " + base + " " + contraries.get(negatives.get(verb)) + " " + adjective + ", but " + contrary(pronoun, verb) + " " + antonym(adjective) + "");
                answers.add(a3);
            }
        }

        return answers;
    }

    private String antonym(String word) {
        for (Map.Entry<String, String> entry : antonyms.entrySet()) {
            if (entry.getKey().equals(word)) {
                return entry.getValue();
            } else if (entry.getValue().equals(word)) {
                return entry.getKey();
            }
        }
        return PLACEHOLDER;
    }

    private String getPronoun(String noun) {
        return pronouns.get(0);
    }

    private String contrary(String noun, String verb) {
        if (verb.equals("is")) {
            return noun + "'s";
        }
        return PLACEHOLDER;
    }
}
